﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _3D_FileExplorer
{
    public class FileSystemDataProvider : TreeDataProvider
    {

        private static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();
        private ContentRating cr = ContentRating.DeserializeFromXML();
        private Parameters par = Parameters.DeserializeFromXML();

        public FileSystemDataProvider() {
        }

        public Node getNode(String path)
        {
            if (path.Substring(0, 1) == "%")
            {
                path = Environment.GetEnvironmentVariable(path.Replace("%",""));
            }
            if (!File.Exists(path) && !Directory.Exists(path))
            {
                throw new FileNotFoundException();
            }
            if(File.Exists(path)) {
                return new LeafNode(path, GetWeight(GetType(path)), GetType(path), GetShortName(path), GetDescription(path));
            } else {
                return new InnerNode(path, 1.0f, GetType(path), GetShortName(path), GetDescription(path));
            }
        }

        public List<Node> getChildrenOfNode(String path) {
            string[] files = null;
            string[] dirs = null;
            List<Node> tempNode = new List<Node>();
            try
            {
                files = Directory.GetFiles(path);
            }
            // This is thrown if even one of the files requires permissions greater
            // than the application provides.
            catch (UnauthorizedAccessException e)
            {
                // This code just writes out the message and continues to recurse.
                // You may decide to do something different here. For example, you
                // can try to elevate your privileges and access the file again.
                log.Add(e.Message);
            }

            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }

            if (files != null)
            {
                int i = 0;
                foreach (string fi in files)
                {
                    if (i <= par.maxFiles)
                    {
                        // String relPath = path + "/" + GetShortName(fi);
                        tempNode.Add(new LeafNode(fi/*relPath*/, GetWeight(GetType(fi)), GetType(fi), GetShortName(fi), GetDescription(fi)));
                        i++;
                    }
                    else
                    {
                        break;
                    }
                
                }


                // Now find all the subdirectories under this directory.
                dirs = Directory.GetDirectories(path);
 i = 0;
                foreach (string dirInfo in dirs)
                {
                    if (i <= par.maxDirs)
                    {
                   // String relPath = (String.IsNullOrEmpty(path) ? GetShortName(dirInfo) : (path + "/" + GetShortName(dirInfo)));
                    tempNode.Add(new InnerNode(dirInfo/*relPath*/, 1, GetType(dirInfo), GetShortName(dirInfo), GetDescription(dirInfo)));
                    i++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            
            return tempNode;
        }

        public string GetType(string name)
        {
            string extension = Path.GetExtension(name);
            // remove leading dot...
            if (!String.IsNullOrEmpty(extension)) extension = extension.Substring(1);
            return extension.ToLower();
        }

        public string GetShortName(string name)
        {
            string shortName = Path.GetFileName(name);
            //shortName = shortName.Substring(0, shortName.Length - 4);
            if (shortName.Length > 10)
            {
                shortName = shortName.Substring(0, 10) + "...";
            }
            return shortName;
        }

        public bool VerifyPath(string path)
        {
            if (Directory.Exists(path))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string GetNoDotsShortName(string name)
        {
            return name = Path.GetFileName(name);
        }
        public string GetName(string id)
        {
            return id;
        }
        public string GetDescription(string id)
        {
            return null;
        }

        public string GetAncestorsPath(string path)
        {
            return Directory.GetParent(path).ToString();
        }

        public float GetWeight(string type)
        {
            if (Fe.par.ContentRating == true)
            {
                return cr.GetRating(type);
            }
            return 1;
        }
    }
}
